using System.Collections.Generic;
using System.Data;
using Sixeyed.CodeGeneration.Metadata.Bases;
using Sixeyed.CodeGeneration.Metadata.Database.Items;
using Sixeyed.CodeGeneration.Metadata.Enums;
using Sixeyed.CodeGeneration.Metadata.Interfaces;
using Sixeyed.CodeGeneration.Metadata.Database.Providers.Bases;

namespace Sixeyed.CodeGeneration.Metadata.Database.Providers
{
    /// <summary>
    /// Provider for returning <see cref="QueryResult"/> metadata items
    /// </summary>
    /// <remarks>
    /// Provider takes user input for the SQL query, so the selection type is input.
    /// Query string is used as the key for the metadata item.
    /// </remarks>
    public class QueryResultProvider : DatabaseProviderBase
    {
        #region Constructors

        /// <summary>
        /// Default constructor
        /// </summary>
        public QueryResultProvider() { }

        #endregion

        #region Overriden members

        /// <summary>
        /// Gets the name of the provider
        /// </summary>
        public override string Name
        {
            get { return "Query Result Provider"; }
        }

        /// <summary>
        /// Gets the description of the provider
        /// </summary>
        public override string Description
        {
            get { return "Retrieves the results of a SQL query"; }
        }

        /// <summary>
        /// Gets the selection type of the provider
        /// </summary>
        public override ProviderSelectionType SelectionType
        {
            get { return ProviderSelectionType.Input; }
        }

        /// <summary>
        /// Gets the name of the metadata item type
        /// </summary>
        public override string ItemTypeName
        {
            get { return  "Query Result"; }
        }

        /// <summary>
        /// Returns a single item name, identifying query result
        /// </summary>
        /// <returns>Query result name</returns>
        public override List<string> GetItemNames()
        {
            List<string> itemNames = new List<string>();
            //this provider does not represent a collection, so single item available:
            itemNames.Add("Results DataSet");
            return itemNames;
        }

        /// <summary>
        /// Returns a populated metadata item
        /// </summary>
        /// <param name="itemKey">SQL query to execute</param>
        /// <returns>Populated <see cref="QueryResult"/></returns>
        public override IMetadataItem GetItem(string itemKey)
        {
            QueryResult item = this.CreateItem<QueryResult>(itemKey);
            if (!string.IsNullOrEmpty(itemKey))
            {
                IDbCommand command = this._source.GetItemCommand(DatabaseItemType.QueryResult, itemKey);
                //get the data:
                DataSet result = this._source.GetData(command);
                item.Item = result;
            }
            return item;
        }

        #endregion
    }
}